# Contributor: Carlo Landmeter <clandmeter@alpinelinux.org>
# Maintainer: Kevin Daudt <kdaudt@alpinelinux.org>
pkgname=netdata
pkgver=1.43.2
pkgrel=2
pkgdesc="Real-time performance monitoring"
url="https://www.netdata.cloud/"
license="GPL-3.0-or-later"
arch="all"
depends="bash"
makedepends="
	abseil-cpp-dev
	autoconf
	automake
	e2fsprogs-dev
	json-c-dev
	libcap-dev
	libuv-dev
	linux-headers
	lz4-dev
	openssl-dev>3
	protobuf-dev
	yaml-dev
	zlib-dev
	"
checkdepends="cmocka-dev"
pkgusers="netdata"
pkggroups="netdata"
subpackages="$pkgname-openrc $pkgname-python::noarch"
install="$pkgname.pre-install"
options="!check" # Broken tests
_aclk_schemas_commit=83c661c0dcddb9526814ebbd0668fbc3e281f03f
_ml_dlib_commit=021cbbb1c2ddec39d8dd4cb6abfbbafdf1cf4482
_mqtt_websockets_commit=6664789ccf57c95451638364f544d1679efd910e
# This _h2o_commit fixes CVE-2023-44487, and does not follow upstream
_h2o_commit=094a6f90f76eeb280a430200cba47bde39b0b8e8
_c_rbuf_commit=51485be511f8c1d592b699f09f7c783aa2270375
_c_rhash_commit=f6c21836b921535ab818c7f72b77346d25f60d8e
source="$pkgname-$pkgver.tar.gz::https://github.com/netdata/netdata/archive/v$pkgver.tar.gz
	aclk-schemas-$_aclk_schemas_commit.tar.gz::https://github.com/netdata/aclk-schemas/archive/$_aclk_schemas_commit.tar.gz
	dlib-$_ml_dlib_commit.tar.gz::https://github.com/davisking/dlib/archive/$_ml_dlib_commit.tar.gz
	mqtt_websockets-$_mqtt_websockets_commit.tar.gz::https://github.com/underhood/mqtt_websockets/archive/$_mqtt_websockets_commit.tar.gz
	h2o-$_h2o_commit.tar.gz::https://github.com/h2o/h2o/archive/$_h2o_commit.tar.gz
	c-rbuf-$_c_rbuf_commit.tar.gz::https://github.com/underhood/c-rbuf/archive/$_c_rbuf_commit.tar.gz
	c_rhash-$_c_rhash_commit.tar.gz::https://github.com/underhood/c_rhash/archive/$_c_rhash_commit.tar.gz
	cxx17-2.patch
	protobuf-23.patch
	$pkgname.initd
	$pkgname.confd
	"

# secfixes:
#   1.43.2-r1:
#     - CVE-2023-44487

copy_submodule() {
	local name=$1
	local commit=$2
	local dest=$3

	cp -r \
		$srcdir/$name-$commit \
		-T "$builddir"/$dest

}

prepare() {
	copy_submodule aclk-schemas "$_aclk_schemas_commit" aclk/aclk-schemas
	copy_submodule dlib "$_ml_dlib_commit" ml/dlib
	copy_submodule mqtt_websockets "$_mqtt_websockets_commit" mqtt_websockets
	copy_submodule h2o "$_h2o_commit" web/server/h2o/libh2o
	copy_submodule c-rbuf "$_c_rbuf_commit" mqtt_websockets/c-rbuf
	copy_submodule c_rhash "$_c_rhash_commit" mqtt_websockets/c_rhash

	default_prepare
	autoreconf -i
}

build() {
	CXXFLAGS="$CXXFLAGS -std=c++17" \
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--with-zlib \
		--with-math \
		--enable-https \
		--with-webdir=/usr/share/webapps/netdata \
		--with-user=netdata
	make
}

check() {
	msg "check"
	make check
}

package() {
	make DESTDIR="$pkgdir" install
	chgrp -R $pkggroups "$pkgdir"/usr/share/webapps/netdata
	install -Dm755 $srcdir/$pkgname.initd $pkgdir/etc/init.d/$pkgname
	install -Dm644 $srcdir/$pkgname.confd $pkgdir/etc/conf.d/$pkgname
	install -Dm644 ./system/netdata.conf $pkgdir/etc/netdata/netdata.conf

	install -d -m0750 -o netdata -g netdata \
		"$pkgdir"/var/lib/netdata \
		"$pkgdir"/var/log/netdata \
		"$pkgdir"/var/cache/netdata
}

python() {
	pkgdesc="$pkgdesc (python3 support)"
	depends="$pkgname python3"

	mkdir -p "$subpkgdir"/etc/netdata
	mkdir -p "$subpkgdir"/usr/libexec/netdata/plugins.d
	mkdir -p "$subpkgdir"/usr/lib/netdata/conf.d

	mv $pkgdir/usr/libexec/netdata/python.d \
		"$subpkgdir"/usr/libexec/netdata
	mv $pkgdir/usr/libexec/netdata/plugins.d/python.d.plugin \
		"$subpkgdir"/usr/libexec/netdata/plugins.d
	mv $pkgdir/etc/netdata/python.d \
		$subpkgdir/etc/netdata/
	mv $pkgdir/usr/lib/netdata/conf.d/python.d \
		$subpkgdir/usr/lib/netdata/conf.d
	mv $pkgdir/usr/lib/netdata/conf.d/python.d.conf \
		$subpkgdir/usr/lib/netdata/conf.d
}

sha512sums="
bf2b0b18af5764f0b7529e75ceb0e7c526d57087ce9c76972d96d5201f41970911af8a575c981c4e3256ee40dcc7788967bd4db9577207df617c031bf7405fb1  netdata-1.43.2.tar.gz
9687ede999002e73563c118fb604cbf8957670f8fac8f7c51d637c1518c00c1919e3b66bf055f152cda766695fc625a751db19a91174f487c0cc2752d47afb52  aclk-schemas-83c661c0dcddb9526814ebbd0668fbc3e281f03f.tar.gz
814e3ee5811b43349a218c2b6595e53c1daf5db2c9346b2ec0ff6475cc83b1803adad3f3210f51e219d6523fcf839543087b94eb18501ee144128fb169bd9c0b  dlib-021cbbb1c2ddec39d8dd4cb6abfbbafdf1cf4482.tar.gz
26dc950804887982fe4472a6c5ce78e36ec89b75fb5e94a8ad9eac3abb7558e2e151cd10652e159910af78da13de054011f266a4cec22becb6f488154ee5737b  mqtt_websockets-6664789ccf57c95451638364f544d1679efd910e.tar.gz
f494a2bf75fe7a6c97bf64ae8d2a4eabe6bcc5c1a4a038cb27c23b478a65a543f9c2dcc8223b0e05ebe2ee811baa06876d13d5abb344c1bc51bdf5e3e8d3a75b  h2o-094a6f90f76eeb280a430200cba47bde39b0b8e8.tar.gz
c65c3c72c60654ef575f485b45c0389198a91b307f39ce984baa4d2fc519c62d645fd226f076b2ec059a97599be8892a8270ffb57bba575c0390181244aff228  c-rbuf-51485be511f8c1d592b699f09f7c783aa2270375.tar.gz
2082de803895a1499006e683f3173205b5033c9f788f1d5a5944463c1da5bc8d8661a187e7a0ec23f9f0e0251557741f72d54a7732a3ad010930e16fa04a0853  c_rhash-f6c21836b921535ab818c7f72b77346d25f60d8e.tar.gz
39cc473fbfff28862a9c3dbbfe59005c57ccee6664bb328924c2124a8f1faeb5e6247a6405a7e498c01a975e826582c28e66b7dd4552b8923ded446bf42609cd  cxx17-2.patch
cfe712c6038f483fd276633330bb0cfc92c2779585e1855295da0d54e7f380d4a201a8bd25b939b69df65a8fe5a1dfa6debbcd9f4e8ca88c1caad3869fcc55a0  protobuf-23.patch
89d5e31a9ff05678bd42b79777069137bb7cb140079fb3c8a8ca52ffdcad0f800eef7a458c065a2ef10fbdd01e96ce5321b59a3d57a7528aa874a49d5a88a8b8  netdata.initd
990ea3f485d9d5ffda50e5f6169704a209d51d18a5d75f97de742b8539c764745f9f59489007d5d1091cdf9f25e8e46519c32fc43cfe527fe2fc8426b0344612  netdata.confd
"
